/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  if (prices.length <= 1) return 0
  let profit = 0
  let i = 0
  let j = 1
  while (j < prices.length) {
    let tmpProfit = prices[j] - prices[i]
    if (tmpProfit <= 0) {
      i++
      j++
    } else {
      while (j < prices.length - 1 && prices[j + 1] >= prices[j]) {
        j++
      }
      profit += prices[j] - prices[i]
      i = j
      j = j + 1
    }
  }
  return profit
};
maxProfit([7,6,4,3,1])